package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.itheima.reggie.entity.Result;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    //验证验证码
    public Result<User> Login(String phone, String requestCode, String sessionCode) {
        if (StrUtil.isBlank(requestCode)){
            throw new BusinessException("输入验证码为空");
        }
        if (StrUtil.isBlank(sessionCode)){
            throw new BusinessException("当前号码没有获取验证码");
        }
        if( !StrUtil.equals(requestCode , sessionCode )){
            throw new BusinessException("验证码不正确");
        }
        //验证码正确，就要判断是否已经有注册，没有的话就注册
        User user = userMapper.selectUserByPhone(phone);
        //要是用户为空，就要添加
        if(user == null){
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userMapper.addUser(user);
        }
        return Result.success(user);
    }
}
